package main

import (
	"fmt"
	"sort"
)

func threesum(nums []int) [][]int {
	var result [][]int

	if nums == nil || len(nums) == 0 {
		return result
	}

	//排序
	sort.Ints(nums)

	left := 0
	right := len(nums) - 1

	for i := 0; i < len(nums); i++ {
		left = i + 1
		for left < right {
			if nums[i]+nums[left]+nums[right] > 0 {
				right--
			} else if nums[i]+nums[left]+nums[right] < 0 {
				left++
			} else {
				var temporaryList []int
				temporaryList = append(temporaryList, nums[i])
				temporaryList = append(temporaryList, nums[left])
				temporaryList = append(temporaryList, nums[right])

				result = append(result, temporaryList)

				left++
				right--
			}
		}

	}

	return result
}

func main() {
	nums := []int{-1, 0, 1, 2, -1, -4}

	res := threesum(nums)

	fmt.Println(res)
}
